package com.servlet;

import com.bean.User;
import com.dao.UserDao;
import com.utils.MD5Util;
import com.utils.ResponseUtils;
import com.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName LoginServlet
 * @Desc 前台登录控制类
 * @Author xiaoding
 * @Date 2021/1/13 20:45
 * @Version 1.0
 */
@WebServlet(name = "LoginServlet",value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private UserDao userDao = new UserDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        if (str.equals("login")) {
            //获取用户名、密码、验证码
            String name = request.getParameter("name").trim();
            String pws = request.getParameter("pws").trim();
            String yzm = request.getParameter("yzm").trim();
            //定义状态、路径
            String message = null;
            String url = null;

            //截取字符串
            String jurisdiction = name.substring(0,2);
            //判断用户权限
            if (jurisdiction.equals("ad")) {
                response.sendRedirect(request.getContextPath()+"/NextLoginServlet?flag="+str+"&name="+name+"&pws="+pws+"&yzm="+yzm);
            }else {
                //判断用户名、密码是否正确
                User user = userDao.LoginFlag(new User(name, MD5Util.MD5(pws)));

                //判断用户是否为空
                if (user != null) {
                    //系统验证码
                    String yzm2 = (String) request.getSession().getAttribute("checkcode");

                    //判断验证码是否相同
                    if (yzm.equals(yzm2)) {
                        message = "登录成功";
                        url = "BookServlet?flag=select";

                        //设置为加密前的密码
                        user.setPws(pws);
                        //将用户保存到浏览器中和Cookie中
                        request.getSession().setAttribute("user",user);
                        //创建cookie将用户名和密码存储
                        Cookie cookie = new Cookie("login", name + ":" + pws);
                        System.out.println(name);
                        System.out.println(pws);
                        cookie.setPath("/");
                        cookie.setMaxAge(24*60*60*1000);
                        response.addCookie(cookie);

                    }else {
                        message = "验证码不正确";
                        url = "login.jsp";
                    }
                }else {
                    message = "用户名或密码不正确";
                    url = "login.jsp";
                }
            }

            //调用跳转的方法
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("reg")) {
            //获取参数
            String phone = request.getParameter("phone");
            //定义状态、路径
            String message = null;
            String url = null;

            //截取字符串
            String jurisdiction = phone.substring(0,2);
            //判断是否是注册管理员
            if (jurisdiction.equals("ad")) {
                message = "您没有资格注册管理员，想要注册管理员，请联系管理员";
                url = "reg.jsp";
            }else {
                String pws = MD5Util.MD5(request.getParameter("pws"));
                String uName = request.getParameter("name");
                String email = request.getParameter("email");
                int count = 25;
                int age = Integer.parseInt(request.getParameter("age"));
                String sex = request.getParameter("sex");
                //判断性别

                System.out.println(new User(phone,pws,uName,email,count,age,sex));
                //调用注册方法
                int a = userDao.addUser(new User(phone,pws,uName,email,count,age,sex));


                //判断是否注册成功
                if ( a > 0) {
                    message = "注册成功";
                    url = "login.jsp";
                }else {
                    message = "注册失败";
                    url = "reg.jsp";
                }
            }
            //调用跳转的方法
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("exit")) {
            //设置回话
            request.getSession().setAttribute("user",null);

            //清除cookie存储用户和密码
            Cookie cookie = new Cookie("login", null);
            cookie.setPath("/");
            cookie.setMaxAge(0);
            response.addCookie(cookie);

            //跳转
            ResponseUtils.writeMessage(response, "退出成功", "login.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
